import { BasicMarker } from '../../js/BasicMarker.js';
import { BasicMarkers } from '../../js/BasicMarkers.js';
import { getMarkerIcon } from '../../js/icon.js';
import { realityHouseDialog } from '../../../../views/reality-house/index.js';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
import { handleCommonRes } from '@gis/lib/utils/resolver.js';
import { getPeopleInfoMapYardHouse } from '@gis/lib/api';
import merge from 'lodash-es/merge.js';

class Marker extends BasicMarker {
  constructor({ map, data }) {
    super({ map, data });
    /* this.type: 这个type主要用于筛选,需要和filter-legend里面的type相对应 */
    this.type = 'cysmartxq';
    super.create();
  }
  getIcon() {
    return getMarkerIcon(this.type);
  }
  getLabel() {
    const label = super.getLabel();
    label.content = `
      <div class="marker_label">
        <p class="marker_label__name">${this.data.yard_name}</p>
        <p class="marker_label__count">房屋数：${this.data.room_total}</p>
      </div>`;
    return label;
  }
  getIconOption(icon) {
    return {
      content: `<div style="width: 52px;height: 96px;background: url(${icon}) center center no-repeat;background-size: 100% 100%"></div>`,
      offset: new AMap.Pixel(0, 0),
    };
  }
  onClick() {
    async function getYardListAsync() {
      const { area_code } = useUserInfoStore();
      const params = {
        area_code: area_code,
        is_smart_community: true,
      };
      const data = await handleCommonRes(getPeopleInfoMapYardHouse, params);
      return data
        .filter((item) => item.longitude && item.latitude)
        .map((item) => merge({}, item, { key: item.yard_id }));
    }
    realityHouseDialog('小区详情', { yard_id: this.data.yard_id, getYardListAsync: getYardListAsync });
  }
}

export default class Cysmartxq extends BasicMarkers {
  constructor({ map }) {
    super();
    this.map = map;
  }
  createMarker(data) {
    return new Marker({ map: this.map, data });
  }
  async getDataAsync() {
    const { area_code } = useUserInfoStore();
    const params = {
      area_code: area_code,
      is_smart_community: true,
    };
    const data = await handleCommonRes(getPeopleInfoMapYardHouse, params);
    return data
      .filter((item) => item.longitude && item.latitude)
      .map((item) => Object.assign(item, { lnglat: [item.longitude, item.latitude] }));
  }
}
